package com.bawan.vims.jmx;

import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class JmxAgent {
	public static void main(String[] args) throws MalformedObjectNameException,  
	    NullPointerException, InstanceAlreadyExistsException,  
	    MBeanRegistrationException, NotCompliantMBeanException, IOException {
		int rmiPort = 1099;
		String jmxServerName = "ZCCJMXServer";
		
		Registry registry = LocateRegistry.createRegistry(rmiPort);
		
		MBeanServer mbServer = MBeanServerFactory.createMBeanServer(jmxServerName);
		
		
		HtmlAdaptorServer adapter = new HtmlAdaptorServer();
		ObjectName adapterName;
		adapterName = new ObjectName(jmxServerName + ":name=" + "htmladapter");
		adapter.setPort(8082);
		adapter.start();
		mbServer.registerMBean(adapter, adapterName);
		
		ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
		mbServer.registerMBean(new Jmx(), objName);
		
		JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
		System.out.println("JMXServiceURL: " + url.toString());
		JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbServer);
		jmxConnServer.start();
	}
}
